<?php
	class docs extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			
			$config = array(
				'upload_path'		=>	realpath(FCPATH.'uploads/docs/'),
				'allowed_types'		=>	'pdf|txt|doc',
				'encrypt_name'		=>	TRUE
			);
			
			$this->load->library('upload',$config);
			
			$user =& Users::getInstance();
			
			if ($user->level < 1) {
				recirect('/');
			}
			
			$this->load->helper('url');
			$this->load->helper('form');
			$this->load->model('Jobs');
		}
		
		public function index()
		{
			$this->load->library('Menu');
			$menu = new Menu;
			$user =& Users::getInstance();
			$data = $menu->gen();
			$data['user'] = $user;
			
			$data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
			$data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
			
			$this->db->order_by("id","DESC");
			$query = $this->db->get('docs');
			$data['docs'] = $query->result();
			
			for ($i = 0; $i < count($data['docs']); $i++) {
				$ext = pathinfo(realpath(FCPATH.'uploads/docs/'.$data['docs'][$i]->file), PATHINFO_EXTENSION);
				if ($ext == "bmp" || $ext == "jpg" || $ext == "jpeg" || $ext == "gif" || $ext == "png") {
					$ext = "img";
				}
				
				$data['docs'][$i]->{"ext"} = $ext;
			}
			
			$this->load->view('docs.php', $data);
		}
		
		public function upload()
		{
			if($this->upload->do_upload('userfile'))
			{
				$data = $this->upload->data();
				//rename($data['file_path'].$data['file_name'],$data['file_path'].$this->input->post('filename').$data['file_ext']);
				
				$this->db->insert('docs',array(
					'name'	=>	$this->input->post('filename'),
					'file'	=>	$data['file_name']
				));
				
				redirect("terms");
				//$this->load->view('admin/upload/success.php');
			}
			else
			{
				print '<pre>';	print_r($this->upload->data());	print '</pre>';
				//$this->load->view('admin/upload/error.php');
				redirect("terms");
			}
		}
		
		public function view()
		{
			$this->db->order_by("id","DESC");
			$query = $this->db->get('docs');
			$data['docs'] = $query->result();
			
			$this->load->view('admin/viewdocs.php',$data);
		}
		
		public function remove($id)
		{
			if(isset($id) && $id > 0)
			{
				if($this->db->delete('docs',array('id' => $id)))
				{
					redirect('terms');
				}
			}
		}
	}
?>